<?php
/**
 * Created By base
 * Author: Virus
 * Date: 2020/3/11
 * Time: 13:51
 */

namespace app\common\model\mysql;


class AuthRule extends BaseModel
{
    protected $hidden = ['update_time'];

    /**
     * 状态获取器
     * @param $value
     * @return string
     */
    public function getStatusAttr($value)
    {
        return $value ? "<i class='fa fa-check-circle'></i>" : "<i class='fa fa-close'></i>";
    }

    /**
     * 菜单获取器
     * @param $value
     * @return string
     */
    public function getIsMenuAttr($value)
    {
        return $value ? "是" : "否";
    }

    /**
     * 保存数据
     * @param $data
     * @return bool
     */
    public function saveData($data)
    {
        if (empty($data) || !is_array($data)) {
            return false;
        }

        return $this->allowField(true)->save($data);
    }

    /**
     * @param $id
     * @param $data
     * @return bool|int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function updateDataById($id, $data)
    {
        $id = intval($id);
        if (empty($data) || !is_array($data) || empty($id)) {
            return false;
        }

        $where = ['id' => $id];

        return $this->where($where)->update($data);
    }

    public function deleteById($id)
    {
        $id = intval($id);
        if (empty($id)) {
            return false;
        }
        $where = [
            'id' => $id,
        ];

        return $this->where($where)->delete();
    }
}